package com.cherry.common.exception;


import com.cherry.common.exception.enums.CommonErrorCode;

/**
 * 服务异常
 *
 * @author sinbad.cheng
 * @since 2022-01-14
 */
public class ServiceException extends RuntimeException implements IErrorCode {

    /**
     * 错误编码
     */
    private final int code;

    /**
     * 消息格式参数
     */
    private final Object[] args;

    public ServiceException(final IErrorCode errorCode, Object... args) {
        super(errorCode.getMsg());
        this.code = errorCode.getCode();
        this.args = args;
    }

    public ServiceException(int code, String msg, Object... args) {
        super(msg);
        this.code = code;
        this.args = args;
    }

    public ServiceException(String msg, Object... args) {
        super(msg);
        this.code = CommonErrorCode.FAILURE.getCode();
        this.args = args;
    }

    public ServiceException(IErrorCode errorCode, String message, Object... args) {
        super(message);
        this.code = errorCode.getCode();
        this.args = args;
    }

    public ServiceException(int code, String message, Throwable cause, Object... args) {
        super(message, cause);
        this.code = code;
        this.args = args;
    }

    public ServiceException(IErrorCode errorCode, Throwable cause, Object... args) {
        this(errorCode.getCode(), errorCode.getMsg(), cause, args);
    }

    public int getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return this.getMessage();
    }

    public Object[] getArgs() {
        return args;
    }

}